package objectstorage

import (
	"net/http"
	"testing"
)

func Test_hexSHA256(t *testing.T) {
	type args struct {
		bb []byte
	}
	tests := []struct {
		name string
		args args
		want string
	}{
		{name: "empty",
			args: args{bb: []byte("")},
			// https://en.wikipedia.org/wiki/SHA-2#Test_vectors
			want: "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := hexSHA256(tt.args.bb); got != tt.want {
				t.Errorf("hexSHA256() = %v, want %v", got, tt.want)
			}
		})
	}
}

func TestCanonicalQueryString(t *testing.T) {
	type args struct {
		rq string
	}
	tests := []struct {
		name    string
		args    args
		want    string
		wantErr bool
	}{
		{name: "empty",
			args: args{rq: ""},
			want: "",
		},
		{name: "not empty",
			args: args{rq: "tag=my-tag&project=my-project"},
			want: "project=my-project&tag=my-tag",
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got, err := CanonicalQueryString(tt.args.rq)
			if (err != nil) != tt.wantErr {
				t.Errorf("CanonicalQueryString() error = %v, wantErr %v", err, tt.wantErr)
				return
			}
			if got != tt.want {
				t.Errorf("CanonicalQueryString() = %v, want %v", got, tt.want)
			}
		})
	}
}

func TestCanonicalAndSignedHeaders(t *testing.T) {
	type args struct {
		header     http.Header
		keysToSign []string
	}
	tests := []struct {
		name    string
		args    args
		want    string
		want1   string
		wantErr bool
	}{
		{name: "empty header",
			args:    args{header: http.Header{}},
			wantErr: true,
		},
		{name: "empty keysToSign",
			args: args{
				header: http.Header{
					"Host": []string{"localhost"},
				},
			},
			wantErr: true,
		},
		{name: "no keysToSign in header",
			args: args{
				header: http.Header{
					"Host": []string{"localhost"},
				},
				keysToSign: []string{"Content-Type"},
			},
			wantErr: true,
		},
		{name: "not empty",
			args: args{
				header: http.Header{
					"X-Auth-Token": []string{"<PASSWORD>"},
					"Content-Type": []string{"application/json"},
					"Host":         []string{"localhost"},
				},
				keysToSign: []string{"Content-Type", "HOST"},
			},
			want:  "content-type:application/json\nhost:localhost",
			want1: "content-type;host",
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got, got1, err := CanonicalAndSignedHeaders(tt.args.header, tt.args.keysToSign)
			if (err != nil) != tt.wantErr {
				t.Errorf("CanonicalAndSignedHeaders() error = %v, wantErr %v", err, tt.wantErr)
				return
			}
			if got != tt.want {
				t.Errorf("CanonicalAndSignedHeaders() got = %v, want %v", got, tt.want)
			}
			if got1 != tt.want1 {
				t.Errorf("CanonicalAndSignedHeaders() got1 = %v, want %v", got1, tt.want1)
			}
		})
	}
}
